<template>
  <el-card>
    <el-row justify="space-between" type="flex">
      <el-form :inline="true">
        <el-form-item label="角色名称">
          <el-input placeholder="请输入角色名称" v-model="queryData.role_name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="initData">搜索</el-button>
          <el-button size="small" icon="el-icon-refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          plain
          @click="$refs.RoleTools.dialogVisible=true"
        >新增</el-button>
      </el-row>
    </el-row>
    <br />
    <!-- 表格区域 -->
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="id" label="角色ID"></el-table-column>
      <el-table-column prop="role_name" label="角色名称"></el-table-column>
      <el-table-column prop="description" label="角色描述"></el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button
            @click="editHandler(row)"
            circle
            size="small"
            type="primary"
            icon="el-icon-edit"
          ></el-button>
          <el-button
            @click="delHandler(row.id)"
            circle
            size="small"
            type="danger"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[3,5,10,20,30]"
      :page-size="queryData.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 弹出框 -->
    <role-tools ref="RoleTools" @updateList="initData" />
  </el-card>
</template>

<script>
import {
  getRoleListApi,
  delRoleApi,
  editRoleInfoApi
} from "@/api/permission/role";
import RoleTools from "./component/role-tools";
import { gettreenode } from "@/utils";

export default {
  name: "Role",
  components: {
    RoleTools
  },
  props: {},
  data() {
    return {
      list: [],
      queryData: {
        role_name: "",
        page: 1,
        limit: 20
      },
      total: 0
    };
  },
  computed: {},
  watch: {},
  created() {
    this.initData();
  },
  mounted() {},
  methods: {
    async initData() {
      let { list, total } = await getRoleListApi(this.queryData);
      // console.log(list);
      // console.log(total);
      this.list = list;
      this.total = total;
    },
    handleSizeChange(val) {
      // console.log(val);//10,4,8
      this.queryData.limit = val;
      this.initData();
    },
    handleCurrentChange(val) {
      //点击那一页就显示那一页
      // console.log(val);
      this.queryData.page = val;
      this.initData();
    },

    // // 让数据回显
    async editHandler(val) {
      // console.log(val);
      this.$refs.RoleTools.dialogVisible = true;
      //发送请求  获取详情
      let res = await editRoleInfoApi(val.id);
      console.log(res.authlist);
      let arr = []; //处理递归节点将获取的数组进行递归
      arr = gettreenode(res.authlist);
      // console.log(arr);
      this.$nextTick(() => {
        let nodes = [];
        arr.forEach(item => {
          // console.log(item);//数组里面的每个id
          const node = this.$refs.RoleTools.$refs.tree.getNode(item); //得到id里面的每个树节点
          // console.log(node)每个树节点里面有isLeaf是否为true 为true则勾选 添加到数组中
          if (node.isLeaf) {
            //关键，过滤掉不是叶子节点的
            nodes.push(item);
          }
        });
        this.$refs.RoleTools.$refs.tree.setCheckedKeys(nodes, true);
      });

      this.$refs.RoleTools.formData = res;
    },
    //删除标签
    async delHandler(id) {
      await this.$confirm("确认删除吗");
      await delRoleApi(id);
      this.$message.success("删除成功");
      //当前页数大于1并且数组的长度=1让页数减1
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--;
      }
      this.initData();
    },
    //重置搜素
    resetSearch() {
      this.queryData.role_name = "";
      this.initData();
    }
    //
  }
};
</script>
<style lang="less" scoped>
</style>